import QtQuick
import QtQuick.Controls
import Gui.Components

Rectangle {
    id: root

    signal doubleClicked
    signal removeItem

    property bool checked: false
    property string musicName: ""
    property string artistsName: ""
    property string albumSource: ""

    Menu {
        id: _menu
        MenuItem {
            text: "删除"
            onTriggered: {
                root.removeItem();
            }
        }
    }

    MouseArea {
        id: _mouseArea
        anchors.fill: parent
        hoverEnabled: true
        acceptedButtons: Qt.LeftButton | Qt.RightButton

        onDoubleClicked: {
            root.doubleClicked();
        }
        onClicked: mouse => {
            if (mouse.button === Qt.RightButton) {
                _menu.x = mouse.x;
                _menu.y = mouse.y;
                _menu.open();
            }
        }
        Item {
            anchors.fill: parent
            AlbumCover {
                id: _displayCover
                width: 50
                height: 50
                albumSource: root.albumSource
                anchors.verticalCenter: parent.verticalCenter
            }
            Column {
                anchors.verticalCenter: _displayCover.verticalCenter
                anchors.left: _displayCover.right
                anchors.right: parent.right
                spacing: 4
                Label {
                    id: _musicName
                    text: root.musicName
                    elide: Label.ElideRight
                    width: parent.width
                    font {
                        pixelSize: 14
                        bold: true
                    }
                    MToolTip {
                        text: root.musicName
                        x: _musicNameMouseArea.mouseX + 10
                        y: _musicNameMouseArea.mouseY + 10
                        visible: _musicNameMouseArea.containsMouse && _musicName.text.length * 14 > _musicName.width
                    }

                    MouseArea {
                        id: _musicNameMouseArea
                        anchors.fill: parent
                        hoverEnabled: true
                        propagateComposedEvents: true
                    }
                }
                Label {
                    id: _artists
                    text: root.artistsName
                    elide: Label.ElideRight
                    width: parent.width
                    font {
                        pixelSize: 10
                    }
                    MToolTip {
                        text: root.artistsName
                        x: _artistsMouseArea.mouseX + 10
                        y: _artistsMouseArea.mouseY + 10
                        visible: _artistsMouseArea.containsMouse && _artists.text.length * 10 > _artists.width
                    }
                    MouseArea {
                        id: _artistsMouseArea
                        anchors.fill: parent
                        hoverEnabled: true
                        propagateComposedEvents: true
                    }
                }
            }
        }
    }
    color: {
        if (root.checked) {
            return palette.button;
        } else {
            if (_mouseArea.containsMouse) {
                return Qt.alpha(palette.button, 0.5);
            } else {
                return "transparent";
            }
        }
    }
}
